'''
Created on 2011-5-9

@author: sevear
'''
import web;
import xiamiproxy;
import predef;
import logging;
import os;
import json;

def generateErr(errtype):
    errrst = {"errcode":errtype,"errmsg":"","content":""}
    return json.dumps(errrst); 

CLIENT_MIN_VERSION = "1.3.4";
def checkVersion(version):
    minVers = CLIENT_MIN_VERSION.split(".");
    vers = version.split(".");
    for i in range(len(minVers)):
        if(len(vers)<=i):
            return False;
        else: 
            if(int(vers[i])<int(minVers[i])):
                return False;
            elif(int(vers[i])>int(minVers[i])):
                return True;
    return True;

def initlog():
    logging.basicConfig(filename = os.path.join(os.getcwd(), 'log.txt'), level = logging.DEBUG)
    logging.info("start logging");

db = web.database(dbn='mysql', user='tomorrp1_dbchaos', pw='huyue860529', db='tomorrp1_dbchaos', host=predef.DB_HOST,port=3306,charset='utf8',use_unicode=0);

def fixListenUrl(trackInfoDic):
    try:
        xiamiurl = xiamiproxy.searchSong(trackInfoDic["name"], trackInfoDic["aname"], trackInfoDic["artist"]);
    except Exception,data:
        print Exception,":",data;
        
    if(xiamiurl!=None and xiamiurl!=""):
        db.update("track",where="sid=%d"%int(trackInfoDic["sid"]),xiamiurl=xiamiurl);
        print trackInfoDic["name"]+" has updated xiami listen url";
    else:
        print trackInfoDic["name"] +" does not find a xiami location";
        
if __name__ == "__main__": 
#    print str(sys.argv[1]);
    print checkVersion("1.3.1.25");
